<?php
/**
 * ============================================================================
 * @package Forum
 * @author Andy
 * @version 15:23
 * @link https://sjdphp.com
 * ----------------------------------------------------------------------------
 */

use app\common\server\Json;


/** 检测系统环境
 * @return array
 */
function checkEnv(){
    $envList = [
        ['name' => '操作系统','is_limit' => 0,'current' => ''],
        ['name' => 'PHP版本','is_limit' => 7.2,'current' => ''],
        ['name' => '上传附件','is_limit' => 0,'current' => ''],
        ['name' => '磁盘空间','is_limit' => '50M','current' => '']
    ];
    foreach($envList as $key =>  $item){
        if($key == 0){ ## 操作系统
            $envList[$key]["current"] = php_uname('s');
        }
        if($key == 1){ ## PHP版本
            $envList[$key]["current"] = PHP_VERSION;
        }
        if($key == 2){ ## 附件检测
            $envList[$key]["current"] = ini_get('upload_max_filesize');
        }
        if($key == 3){ ## 磁盘空间
            $envList[$key]["current"] = floor(disk_free_space(realpath(dirname(__FILE__) . '/../../')) / (1024 * 1024)) . 'M';
        }
    }
    return $envList;

}

/** 检测文件是否可写
 * @return array
 */
function checkDirFile(){
    $dirList = [
        ['name' => 'addons','path' => '\..\..\addons'],
        ['name' => 'config','path' => '\..\..\config'],
        ['name' => 'storage','path' => '\..\..\public\storage'],
        ["name" => 'template','path' => '\..\..\template'],
        ["name" => 'vendor','path' => '\..\..\vendor'],
        ["name" => 'runtime','path' => '\..\..\runtime'],
        ['name' => 'install','path' => '']
    ];
    $list = [];
    foreach ($dirList as $item) {
        $writeable = is_writeable(__DIR__ . $item["path"]);
        $list[] = [
            "name" => $item["name"],
            "writeable" => $writeable == true ? 1 : 0
        ];
    }
    return $list;
}

/** 检查函数
 * @return array[]
 */
function checkFunc(){
    $funList = [
        [
            'name' => 'Mbstring字符处理',
            'func' => 0,
            'value' => 'mb_substr',
        ],
        [
            'name' => 'Curl传输',
            'func' => 0,
            'value' => 'curl_init',
        ],
        [
            'name' => 'PDO数据库',
            'func' => 0,
            'value' => 'pdo',
        ],
        [
            'name' => 'php_fileinfo',
            'func' => 0,
            'value' => 'finfo_open',
        ],
        [
            'name' => '文件读写',
            'func' => 0,
            'value' => 'file_get_contents',
        ],
        [
            'name' => 'GD图像处理',
            'func' => 0,
            'value' => 'imagecreate',
        ],
        [
            'name' => 'Gzip压缩',
            'func' => 0,
            'value' => 'ob_gzhandler',
        ],
    ];

    foreach($funList as $key => $item){
        if(function_exists($item["value"]) || class_exists($item["value"])){
            $funList[$key]["func"] = 1;
        }else{
            $funList[$key]["func"] = 0;
        }
    }

    return $funList;
}

/** 创建数据库
 * @param $host
 * @param $port
 * @param $username
 * @param $password
 * @param $database
 * @return \think\response\Json
 */
function checkDataBase($host,$port,$username,$password,$database){

}


